{% extends 'base_right.html' %}

{% block bodycontent %}

<section class="content">

            <div id="toolbar" class="toolbar">
                <a href="javascript:;" class="btn btn-primary btn-refresh" ><i class="fa fa-refresh"></i> </a> 
                <a href="javascript:;" id="btnadd" class="btn btn-success btn-add" ><i class="fa fa-plus"></i> 添加</a>                     
            </div>
            <table id="table" class="table table-striped table-bordered table-hover" width="100%">
            </table>

</section>

{% endblock %}

{% block footer %}
<script>
        var toolbar = $("#toolbar");
        var table = $("#table");
        
        // 初始化表格
        table.bootstrapTable({
            url: '/mail/list/group',
            sortName: 'created',
            sortOrder: 'desc',
            cache: false,
            sidePagination: 'server',
            pagination: true,
            method: 'get',
            pageSize: 20,
            pageList: [20, 50, 100, 'All'],
            toolbar: "#toolbar",
            mobileResponsive: true,
            pk: 'id', //editable插件
            search: true,
            searchOnEnterKey: true, //回车查询
            columns: [
                [
                    //{field: 'state', checkbox: true, },
                    //{field: 'id', title: 'ID', visible: false, switchable: false},
                    {field: 'name', title: '显示名称'},
                    {field: 'address', title: '邮件组地址'},
                    {field: 'users', title: '邮件组用户'},
                    /*
                    {field: 'active', title: '状态',formatter:function(value,row,index){  
                        if(row.active){
                            a = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\
                            <a class="text-red" href="#" onclick="diable(\'' + row.username + '\')"><i class="fa fa-stop"></i>停用</a> ';  
                        }else{
                            a = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\
                            <a class="text-green" href="#" onclick="active(\'' + row.username + '\')"><i class="fa fa-play"></i>启用</a> '; 
                        } 
                        return a;  
                        }
                    },*/
                    {field: 'operate', title: '操作', align: 'center',formatter:function(value,row,index){   
                        var a = '<a href="#" onclick="edit(\''+ row.address + '\')"><i class="fa fa-pencil"></i>编辑</a>';   
                        var a = a + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="del(\'' + row.address + '\')"><i class="fa fa-trash"></i>删除</a> ';  
                        return a;  
                        } 
                    }
                ]
            ]
        });

        // 刷新按钮事件
        $(toolbar).on('click', '.btn-refresh', function () {
            table.bootstrapTable('refresh');
        });

        //添加按钮事件
        $(toolbar).on('click', '.btn-add', function () {
            layui_show('添加','/mail/add/group')
        })

        //编辑
        function edit(name){
            layui_show('编辑','/mail/edit/group?address=' + name)
        }

        // 删除按钮事件
        function del(name) {
            var csrftoken = getCookie('csrftoken');
            var data = {"address" : name}

            var indexlayer = layer.confirm(
                    '确定要删除 '+name+' ?',
                    //{icon: 3, title: 'Warning', offset: 0, shadeClose: true},
                    {icon: 3, title: '警告', shadeClose: true},
                    function () {
                        $.ajax({ 
                            url : "/mail/del/group",
                            type: 'POST', 
                            dataType : "json", 
                            data: data, 
                            beforeSend: function(request) {
                                //django需要csrf验证,Forbidden (CSRF cookie not set.):
                                request.setRequestHeader("X-CSRFToken", csrftoken);
                            },
                            success : function(d) { 
                                if (d.code == 1){
                                    toastr.success("操作成功");  
                                    table.bootstrapTable('refresh');
                                    layer.close(indexlayer);
                                }else{
                                    toastr.error(d.msg);  
                                }
                            } 
                        }); 
                        
                    }
            );
        };

        // 停用事件
        function diable(name) {
            var csrftoken = getCookie('csrftoken');
            var data = {"username" : name,"status":0,"type":"user" }

            var indexlayer = layer.confirm(
                    '确定要停用 '+name+' ?',
                    //{icon: 3, title: 'Warning', offset: 0, shadeClose: true},
                    {icon: 3, title: '警告', shadeClose: true},
                    function () {
                        $.ajax({ 
                            url : "/mail/makestatus",
                            type: 'POST', 
                            dataType : "json", 
                            data: data, 
                            beforeSend: function(request) {
                                //django需要csrf验证,Forbidden (CSRF cookie not set.):
                                request.setRequestHeader("X-CSRFToken", csrftoken);
                            },
                            success : function(d) { 
                                if (d.code == 1){
                                    toastr.success("操作成功");  
                                    table.bootstrapTable('refresh');
                                    layer.close(indexlayer);
                                }else{
                                    toastr.error(d.msg);  
                                }
                            } 
                        }); 
                        
                    }
            );
        };
        // 激活事件
        function active(name) {
            var csrftoken = getCookie('csrftoken');
            var data = {"username" : name,"status":1,"type":"user" }

            var indexlayer = layer.confirm(
                    '确定要启用 '+name+' ?',
                    //{icon: 3, title: 'Warning', offset: 0, shadeClose: true},
                    {icon: 3, title: '警告', shadeClose: true},
                    function () {
                        $.ajax({ 
                            url : "/mail/makestatus",
                            type: 'POST', 
                            dataType : "json", 
                            data: data, 
                            beforeSend: function(request) {
                                //django需要csrf验证,Forbidden (CSRF cookie not set.):
                                request.setRequestHeader("X-CSRFToken", csrftoken);
                            },
                            success : function(d) { 
                                if (d.code == 1){
                                    toastr.success("操作成功");  
                                    table.bootstrapTable('refresh');
                                    layer.close(indexlayer);
                                }else{
                                    toastr.error(d.msg);  
                                }
                            } 
                        }); 
                        
                    }
            );
        };
</script>
{% endblock %}